package com.hndzyu.yd.task;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;

import com.google.gson.Gson;
import com.gyf.barlibrary.ImmersionBar;
import com.scwang.smartrefresh.header.MaterialHeader;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.hndzyu.yd.R;
import com.hndzyu.yd.bean.NEW;
import com.hndzyu.yd.bean.UserBean;
import com.hndzyu.yd.customtask.CreditCardDetailsActivity;
import com.hndzyu.yd.customtask.CreditTaskDetailsActivity;
import com.hndzyu.yd.ui.NewAdapter;
import com.hndzyu.yd.url.Path;
import com.hndzyu.yd.utils.OkHttpDownloadJsonUtil;


import java.util.List;

/**
 * Created by zz9527 on 2018/10/25.
 */
public class HighTaskActivity extends Activity {
    private ListView lv;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_hightask);
        ImmersionBar.with(this)
                .fitsSystemWindows(true)  //使用该属性,必须指定状态栏颜色
                .statusBarColor(R.color.colorAccent)
                .init();
        findViewById(R.id.backarea).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        SmartRefreshLayout refresh = findViewById(R.id.refresh);
        refresh.setRefreshHeader(new MaterialHeader(this));
        refresh.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(RefreshLayout refreshlayout) {
                refreshlayout.finishRefresh(2000/*,false*/);//传入false表示刷新失败
                getData();
            }
        });
        lv = findViewById(R.id.lv_high);
        lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {

            }
        });
        getData();
    }

   private  void getData(){
       OkHttpDownloadJsonUtil.downloadJson(this, Path.VIPFAST(UserBean.uid), new OkHttpDownloadJsonUtil.onOkHttpDownloadListener() {
           @Override
           public void onsendJson(String json) {
               Gson gson = new Gson();
               final List<NEW.DataBean> data = gson.fromJson(json, NEW.class).getData();
               NewAdapter newAdapter = new NewAdapter(HighTaskActivity.this,data);
               lv.setAdapter(newAdapter);
               lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                   @Override
                   public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                       NEW.DataBean dataBean = data.get(i);
                       Intent intent = new Intent();
                           switch (dataBean.getFclass()) {
                               case 1://分享
                                   intent.setClass(HighTaskActivity.this, WebActivity.class);
                                   intent.putExtra("url", dataBean.getUrl());
                                   intent.putExtra("shid", dataBean.getId());
                                   intent.putExtra("fen", dataBean.getFen());
                                   intent.putExtra("title", dataBean.getTitle());
                                   intent.putExtra("img", dataBean.getLogo());
                                   intent.putExtra("type", 0);
                                   intent.putExtra("intro", dataBean.getShcon());
                                   intent.putExtra("isshow", dataBean.getIsshow());
                                   intent.putExtra("scon", dataBean.getShowtext());
                                   intent.putExtra("ym",dataBean.getYuming());
                                   break;
                               case 2://关注
                                   intent.setClass(HighTaskActivity.this, FollowActivity.class);
                                   intent.putExtra("iv", dataBean.getLogo());
                                   intent.putExtra("title", dataBean.getTitle());
                                   intent.putExtra("one", dataBean.getDan());
                                   intent.putExtra("all", dataBean.getMoney());
                                   intent.putExtra("wechatnum", dataBean.getWxunid());
                                   intent.putExtra("num", dataBean.getNum()+"");
                                   intent.putExtra("sum", dataBean.getSum()+"");
                                   break;
                               case 3://信用卡
                                   intent.setClass(HighTaskActivity.this, CreditCardDetailsActivity.class);
                                   intent.putExtra("taskid", dataBean.getId());
                                   break;
                               case 4://截图任务
                                   intent.setClass(HighTaskActivity.this, CreditTaskDetailsActivity.class);
                                   intent.putExtra("taskid", dataBean.getId());
                                   break;
                           }
                           startActivity(intent);
                   }
               });
           }
       });
       }
    @Override
    protected void onDestroy() {
        super.onDestroy();
        ImmersionBar.with(this).destroy();
    }
}
